﻿<?php

    date_default_timezone_set("PRC");
    $con = new mysqli("127.0.0.1", "", "", "");
    if ( mysqli_connect_errno ()) {
         die('Could not connect: ' . $con->connect_error);
    }
    $con->query("SET NAMES utf8");

    if (!isset($_POST["danmu"])) {
    	die("no data");
    }

    $text = $_POST["danmu"];
    //$text = mysql_real_escape_string($text);

    $ua = $_SERVER['HTTP_USER_AGENT'];
    $ip = getIP();
    $time = time();
    $info = $ua."\n".$ip; 

    $sql = "INSERT INTO `content` (`text`, `info`, `time`) VALUES ('$text', '$info', '$time')";
    if ($con->query($sql)) {
    	echo "ok";
    }
    else {
    	return "fail";
    }


    function getIP() { 
		global $ip; 

		if (getenv("HTTP_CLIENT_IP")) 
		$ip = getenv("HTTP_CLIENT_IP"); 
		else if(getenv("HTTP_X_FORWARDED_FOR")) 
		$ip = getenv("HTTP_X_FORWARDED_FOR"); 
		else if(getenv("REMOTE_ADDR")) 
		$ip = getenv("REMOTE_ADDR"); 
		else 
		$ip = "Unknow"; 

		return $ip; 
	} 



